package com.wd.tech.view.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.wd.tech.R;
import com.wd.tech.base.BaseActivity;
import com.wd.tech.base.BasePresenter;
import com.wd.tech.bean.LoginBean;
import com.wd.tech.jiami.RsaCoder;
import com.wd.tech.mvp.IPresenterImpl;
import com.wd.tech.url.BaseUrl;
import com.wd.tech.utils.NetUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_pwd)
    EditText etPwd;

    @BindView(R.id.tv_register)
    TextView tvRegister;
    @BindView(R.id.bt_login)
    Button btLogin;
    @BindView(R.id.disanfang)
    TextView disanfang;
    @BindView(R.id.textView1)
    TextView textView1;
    @BindView(R.id.textView2)
    TextView textView2;
    @BindView(R.id.iv_renlian)
    ImageView ivRenlian;
    @BindView(R.id.iv_tup)
    ImageView ivTup;
    @BindView(R.id.wangluobanduan)
    LinearLayout wangluobanduan;
    @BindView(R.id.lin_login)
    LinearLayout linLogin;
    @BindView(R.id.togglePwd)
    ToggleButton togglePwd;
    @BindView(R.id.bt_lianjie)
    Button btLianjie;
    private boolean wang;
    private SharedPreferences sp;

    @Override
    protected void initData() {
        ivRenlian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, FaceActivity.class);
                startActivity(intent);
                finish();
            }
        });
        togglePwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked) {
                    etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });
        wang = NetUtil.getInstance().HasNet();
        sp = getSharedPreferences("login", MODE_PRIVATE);
        tvRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
        btLogin.setOnClickListener(new View.OnClickListener() {

            private String jiapwd;

            @Override
            public void onClick(View view) {
                Map<String, Object> map = new HashMap<>();
                String phone = etPhone.getText().toString();
                if (phone.isEmpty()) {
                    Toast.makeText(LoginActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    map.put("phone", phone);
                }
                String pwd = etPwd.getText().toString();
                try {
                    //公钥加密数据
                    jiapwd = RsaCoder.encryptByPublicKey(pwd);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (pwd.isEmpty()) {
                    Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    map.put("pwd", jiapwd);
                }
                mPresenter.IModelRegisterInfo(BaseUrl.LoginUrl, map, LoginBean.class);
            }
        });
    }

    @Override
    protected void initView() {

    }

    @Override
    protected BasePresenter initPresenter() {
        return new IPresenterImpl();
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void success(Object object) {
        if (wang) {
            if (object instanceof LoginBean) {
                String status = ((LoginBean) object).getStatus();
                if (status.equals("0000")) {
                    Toast.makeText(this, "" + ((LoginBean) object).getMessage(), Toast.LENGTH_SHORT).show();
                    sp.edit().putInt("userid", ((LoginBean) object).getResult().getUserId())
                            .putString("sessionid", ((LoginBean) object).getResult().getSessionId())
                            .putString("username",((LoginBean) object).getResult().getNickName())
                            .putInt("vip",((LoginBean) object).getResult().getWhetherVip())
                            .putString("headpic",((LoginBean) object).getResult().getHeadPic())
                            .commit();
                    Intent intent = new Intent(this, TabTwoActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(this, "" + ((LoginBean) object).getMessage(), Toast.LENGTH_SHORT).show();
                }
            }
        } else {
            wangluobanduan.setVisibility(View.VISIBLE);
            linLogin.setVisibility(View.GONE);
            Toast.makeText(this, "请链接网络~~~", Toast.LENGTH_SHORT).show();
            return;
        }

    }

    @Override
    public void Error(Throwable throwable) {

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.bt_lianjie)
    public void onViewClicked() {
        Intent intent = new Intent();
        intent.setAction("android.net.wifi.PICK_WIFI_NETWORK");
        startActivity(intent);
    }
}
